﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.utils;
using System.Drawing.Printing;

namespace Cashier.prints
{
    public partial class PrinterSettingForm : Form
    {
        private MainForm mainform;
        private PrinterSettings printerSettings;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="printerSettings"></param>
        public PrinterSettingForm(MainForm mainform, PrinterSettings printerSettings)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.printerSettings = printerSettings;

            this.InitialListView();
        }

        /// <summary>
        /// 显示所有本地打印机
        /// </summary>
        private void InitialListView()
        {
            try
            {
                for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++ )
                {
                    String printerName = PrinterSettings.InstalledPrinters[i];
                    listView1.Items.Add(new ListViewItem(printerName));
                    if (printerName.Equals(printerSettings.PrinterName))
                        listView1.Items[i].Selected = true;
                }
                listView1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 选择项事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!button_apply.Enabled)
                button_apply.Enabled = true;
        }

        /// <summary>
        /// 选择打印机
        /// </summary>
        private void SelectedPrinter()
        {
            if (listView1.Items.Count > 0)
            {
                printerSettings.PrinterName = listView1.SelectedItems[0].Text;
            }
            button_apply.Enabled = false;
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_apply_Click(object sender, EventArgs e)
        {
            this.SelectedPrinter();
        }

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.Enter:
                    this.SelectedPrinter();
                    break;
            }
            return b;
        }
    }
}
